package com.hiss.basic.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hiss.basic.client.system.ISysRoleMenuService;
import com.hiss.basic.client.system.ISysRoleService;
import com.hiss.basic.client.system.ISysUserRoleService;
import com.hiss.basic.model.system.request.*;
import com.hiss.basic.model.system.response.SysRoleQueryResponse;
import com.hiss.basic.model.system.response.SysUserQueryResponse;
import com.hiss.basic.starter.core.model.R;
import com.hiss.basic.starter.log.annotation.Log;
import com.hiss.basic.starter.log.enums.BusinessTypeEnum;
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 系统角色 controller
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Resource
    private ISysRoleService sysRoleService;

    @Resource
    private ISysRoleMenuService sysRoleMenuService;

    @Resource
    private ISysUserRoleService sysUserRoleService;

    @SaCheckPermission("sys:role:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R<List<SysRoleQueryResponse>> list(SysRoleQueryRequest request) {
        List<SysRoleQueryResponse> roleList = sysRoleService.findList(request);
        return R.success(roleList);
    }

    @SaCheckPermission("sys:role:list-page")
    @RequestMapping(value = "/list/page", method = RequestMethod.GET)
    public R<Page<SysRoleQueryResponse>> listPage(SysRoleQueryRequest request) {
        Page<SysRoleQueryResponse> roleListPage = sysRoleService.findPageList(request);
        return R.success(roleListPage);
    }

    @SaCheckPermission("sys:role:user-list-page")
    @RequestMapping(value = "/user/list/page", method = RequestMethod.GET)
    public R<Page<SysUserQueryResponse>> userListPage(SysRoleUserQueryRequest request) {
        Page<SysUserQueryResponse> response = sysRoleService.findUserListPage(request);
        return R.success(response);
    }

    @RequestMapping(value = "/menu-ids", method = RequestMethod.GET)
    public R<Collection<Long>> roleMenuIds(Long roleId) {
        Collection<Long> response = sysRoleService.findRoleMenuIds(roleId);
        return R.success(response);
    }

    @SaCheckPermission("sys:role:create")
    @Log(title = "新增角色", businessType = BusinessTypeEnum.INSERT)
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public R<Void> create(@RequestBody SysRoleCreateRequest request) {
        sysRoleService.create(request);
        return R.success();
    }

    @SaCheckPermission("sys:role:update")
    @Log(title = "更新角色", businessType = BusinessTypeEnum.UPDATE)
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public R<Void> update(@RequestBody SysRoleUpdateRequest request) {
        sysRoleService.update(request);
        return R.success();
    }

    @SaCheckPermission("sys:role:authority")
    @Log(title = "角色授权", businessType = BusinessTypeEnum.GRANT)
    @RequestMapping(value = "/authority", method = RequestMethod.POST)
    public R<Void> authority(@RequestBody SysRoleMenuUpdateRequest request) {
        sysRoleMenuService.create(request);
        return R.success();
    }

    @SaCheckPermission("sys:role:remove")
    @Log(title = "删除角色", businessType = BusinessTypeEnum.DELETE)
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public R<Void> remove(Long id) {
        sysRoleService.remove(id);
        return R.success();
    }

    @SaCheckPermission("sys:role:remove-user")
    @Log(title = "删除角色关联用户", businessType = BusinessTypeEnum.DELETE)
    @RequestMapping(value = "/remove/user", method = RequestMethod.DELETE)
    public R<Void> removeUser(Long roleId, Long userId) {
        sysUserRoleService.removeByRoleIdAndUserId(roleId, userId);
        return R.success();
    }
}
